package com.system.borrow.activity;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.system.borrow.R;
import com.system.borrow.bean.GloData;
import com.system.borrow.utils.SecondTitle;
import com.system.borrow.utils.SharedPreferencesUtil;

/**
 * 设置页面
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener{
LinearLayout ll_calback,ll_about,ll_sw,ll_exit,ll_good;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        SecondTitle title = new SecondTitle(this);
        title.setTitle("设置",null);
        initView();
        initListener();
    }

    private void initListener() {
        ll_calback.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ImageView iv_calback = (ImageView) view.findViewById(R.id.iv_calback);
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        iv_calback.setImageResource(R.drawable.go_select);
                        break;
                    case MotionEvent.ACTION_UP:
                        iv_calback.setImageResource(R.drawable.go);
                        break;
                }
                return false;
            }
        });
        ll_calback.setOnClickListener(this);
        ll_about.setOnClickListener(this);
        ll_about.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ImageView iv_calback = (ImageView) view.findViewById(R.id.iv_about);
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        iv_calback.setImageResource(R.drawable.go_select);
                        break;
                    case MotionEvent.ACTION_UP:
                        iv_calback.setImageResource(R.drawable.go);
                        break;
                }
                return false;
            }
        });
        ll_sw.setOnClickListener(this);
        ll_sw.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ImageView iv_calback = (ImageView) view.findViewById(R.id.iv_sw);
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        iv_calback.setImageResource(R.drawable.go_select);
                        break;
                    case MotionEvent.ACTION_UP:
                        iv_calback.setImageResource(R.drawable.go);
                        break;
                }
                return false;
            }
        });
        ll_exit.setOnClickListener(this);
        ll_exit.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ImageView iv_calback = (ImageView) view.findViewById(R.id.iv_exit);
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        iv_calback.setImageResource(R.drawable.go_select);
                        break;
                    case MotionEvent.ACTION_UP:
                        iv_calback.setImageResource(R.drawable.go);
                        break;
                }
                return false;
            }
        });
        ll_good.setOnClickListener(this);
        ll_good.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ImageView iv_calback = (ImageView) view.findViewById(R.id.iv_goods);
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        iv_calback.setImageResource(R.drawable.go_select);
                        break;
                    case MotionEvent.ACTION_UP:
                        iv_calback.setImageResource(R.drawable.go);
                        break;
                }
                return false;
            }
        });
    }

    private void initView() {
        ll_calback = (LinearLayout) findViewById(R.id.ll_calback);
        ll_about = (LinearLayout) findViewById(R.id.ll_about);
        ll_sw = (LinearLayout) findViewById(R.id.ll_sw);
        ll_exit = (LinearLayout) findViewById(R.id.ll_exit);
        ll_good = (LinearLayout)findViewById(R.id.ll_good);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_calback:
                Intent callback = new Intent(this,CallBackActivity.class);
                startActivity(callback);
                break;
            case R.id.ll_about:
                Intent about = new Intent(this,AboutActivity.class);
                startActivity(about);
                break;
            case R.id.ll_sw:
                showDialog();
                break;
            case R.id.ll_good:
                Uri uri = Uri.parse("market://details?id="+getPackageName());
                Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break ;
            case R.id.ll_exit:
                ShowCanheDialog(1);
                break;
        }
    }
    private void showDialog() {
        View v = getLayoutInflater().inflate(R.layout.dialog_hezuo, null);
        TextView tv_sure = (TextView) v.findViewById(R.id.tv_sure);
        final Dialog dialog = new Dialog(this, R.style.RcDialog);
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        WindowManager wm = getWindowManager();
        int mHeight = wm.getDefaultDisplay().getHeight();
        int mWeight = wm.getDefaultDisplay().getWidth();
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(v, new ViewGroup.LayoutParams((int) (mWeight*0.6), ViewGroup.LayoutParams.WRAP_CONTENT));
        dialog.show();
    }

    /**
     * 清理缓存 0和 退出1
     */
    public void ShowCanheDialog(final int type) {
        final Dialog dialog = new Dialog(this, R.style.RcDialog);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(true);
        View view = getLayoutInflater().inflate(R.layout.layout_cache, null);
        Button cancel = (Button) view.findViewById(R.id.cancel);
        Button sure = (Button) view.findViewById(R.id.sure);
        TextView tv_out = (TextView) view.findViewById(R.id.tv_out);
        TextView tv_data_out = (TextView) view.findViewById(R.id.tv_data_out);
        if (type == 0) {
            tv_out.setText("清理缓存");
            tv_data_out.setText("根据缓存文件的大小，清理时间从几秒到几分钟不等，请耐心等待");
        } else {
            tv_out.setText("退出登录");
            tv_data_out.setText("退出当前账号不会删除任何数据，\n下次登录依然可以使用本账号。");
        }
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == 0) {
                } else {
                    GloData.setCustomerDTO(null);
                    SharedPreferencesUtil.putString(SettingActivity.this, "custormer", "custormer", "");
                    Intent intent  = new Intent(SettingActivity.this,LoginActivity.class);
                    startActivity(intent);
                    HomeActivity.homeActivity.exitLogin();
                    finish();
                }
                dialog.dismiss();
            }
        });
        dialog.setContentView(view);
        dialog.show();
    }
}
